<template>
  <div class="summary-outline-wrap">
    <div class="summary-outline-btn">
      <div class="outline-btn-edit">
        <span v-show="!isEdit" class="mr20" @click="editHandler">编辑</span>
        <p class="mr20" v-show="isEdit">
          <span class="mr20" @click="saveHandler">保存</span>
          <span @click="cancelHandler">取消</span>
        </p>
      </div>
      <span class="export-btn" v-pdf="{ el: 'item-detail-p2', title: '项目启动会议纪要' }">导出</span>
      <!-- <div class="print" v-print="{ el: `#gf`, popTitle: '定制信息' }">打印</div> -->
    </div>
    <div class="item-detail-p2">
      <div class="summary-outline-title">项目启动会议纪要</div>
      <div id="gf">
        <div class="summary-outline-content">
          <div class="outline-info">
            <div class="outline-info-title">基本信息</div>
            <ul class="outline-info-list">
              <li>
                <span>客户名称：</span>
                <!-- <span>{{ detail.orderModel?.companyName || "--" }}</span> -->
                <span>{{ detail.companyName || "--" }}</span>
              </li>
              <li>
                <span>项目编号：</span>
                <span>{{ detail.code || "--" }}</span>
              </li>
              <li>
                <span>方案负责人：</span>
                <span>{{ planHead.map(item => item.userName).join(",") || "--" }}</span>
              </li>
              <!-- <li>
                <span>销售负责人：</span>
                <span>{{ detail.orderModel?.saleUserName || "--" }}</span>
              </li> -->
              <!-- <li>
                <span class="info-width">合同日期：</span>
                {{
                  detail?.orderModel?.contractSignTime ? dayJs(detail?.orderModel?.contractSignTime).format("YYYY/MM/DD") : "--"
                }}
              </li> -->
            </ul>
            <ul class="outline-info-list">
              <!-- <li>
                <span class="info-width">发货日期：</span>
                <span>2024/7/12</span>
              </li> -->
            </ul>
          </div>
          <div class="outline-info summary-outline-custom">
            <div class="outline-info-title">参会人员</div>
            <div>
              <el-input :disabled="!isEdit" type="textarea" rows="4" v-model="taskOrigin.remark"></el-input>
            </div>
          </div>
          <div class="outline-info summary-outline-custom">
            <div class="outline-info-title">会议结论</div>
            <div>
              <el-input :disabled="!isEdit" type="textarea" rows="4" v-model="taskOrigin.description"></el-input>
            </div>
          </div>
          <div class="summary-outline-table">
            <TableContent :is-edit="isEdit" title="方案讲解与答疑" :table-options="schemList">
              <template #content="scope">
                <span v-show="!isEdit">{{ scope.scope.content || "--" }}</span>
                <el-input v-show="isEdit" type="textarea" v-model="scope.scope.content" autosize placeholder="问题点"></el-input>
              </template>
              <template #replyContent="scope">
                <span v-show="!isEdit">{{ scope.scope.replyContent || "--" }}</span>
                <el-input
                  type="textarea"
                  v-show="isEdit"
                  v-model="scope.scope.replyContent"
                  autosize
                  placeholder="回复"
                ></el-input>
              </template>
              <template #replyUserId="scope">
                <span v-show="!isEdit">{{ scope.scope.replyUserName || "--" }}</span>
                <el-select v-show="isEdit" v-model="scope.scope.replyUserId" placeholder="请选择回复人员" clearable filterable>
                  <el-option v-for="item in viewUserList" :key="item.name" :label="item.name" :value="item.id" />
                </el-select>
              </template>
              <template #replyTime="scope">
                <span v-show="!isEdit">{{
                  scope.scope.replyTime ? dayJs(scope.scope.replyTime).format("YYYY-MM-DD") : "--"
                }}</span>
                <div v-show="isEdit" style="width: 100%">
                  <el-date-picker
                    type="date"
                    v-model="scope.scope.replyTime"
                    placeholder="回复日期"
                    format="YYYY-MM-DD"
                    value-format="YYYY-MM-DD HH:mm:ss"
                  ></el-date-picker>
                </div>
              </template>
              <template #actionOptions="scope">
                <span class="task-table-del" @click="deleteAgv(1, scope.scope)">删除</span>
              </template>
            </TableContent>
          </div>
          <div class="summary-outline-table">
            <TableContent :is-edit="isEdit" title="待办事项" :table-options="itemList">
              <template #content="scope">
                <span v-show="!isEdit">{{ scope.scope.content || "--" }}</span>
                <el-input type="textarea" v-show="isEdit" v-model="scope.scope.content" autosize placeholder="问题点"></el-input>
              </template>
              <template #replyContent="scope">
                <span v-show="!isEdit">{{ scope.scope.replyContent || "--" }}</span>
                <el-input
                  type="textarea"
                  v-show="isEdit"
                  v-model="scope.scope.replyContent"
                  autosize
                  placeholder="回复"
                ></el-input>
              </template>
              <template #replyUserId="scope">
                <span v-show="!isEdit">{{ scope.scope.replyUserName || "--" }}</span>
                <el-select v-show="isEdit" v-model="scope.scope.replyUserId" placeholder="请选择回复人员" clearable filterable>
                  <el-option v-for="item in viewUserList" :key="item.name" :label="item.name" :value="item.id" />
                </el-select>
              </template>
              <template #replyTime="scope">
                <span v-show="!isEdit">{{
                  scope.scope.replyTime ? dayJs(scope.scope.replyTime).format("YYYY-MM-DD") : "--"
                }}</span>
                <div v-show="isEdit" style="width: 100%">
                  <el-date-picker
                    type="date"
                    v-model="scope.scope.replyTime"
                    placeholder="回复日期"
                    format="YYYY-MM-DD"
                    value-format="YYYY-MM-DD HH:mm:ss"
                  ></el-date-picker>
                </div>
              </template>
              <template #actionOptions="scope">
                <span class="task-table-del" @click="deleteAgv(2, scope.scope)">删除</span>
              </template>
            </TableContent>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { reactive, ref, onMounted, inject } from "vue";
import TableContent from "../components/TableContent/index.vue";
import { getListAllUser } from "@/api/modules/sys";
import { TableProps } from "@/components/Table/interface";
import { ElMessage } from "element-plus";
import mittBus from "@/utils/mittBus";
import {
  getProjectProblemList,
  updateProjectTask,
  getProjectProblemAddList,
  getTaskDetail,
  getProjectUserList
} from "@/api/modules/project"; // getProjectProblemAddList, deleteProjectProduct,
import { useHandleData } from "@/hooks/useHandleData";
import { useConst } from "@/hooks/useConst";
import dayJs from "dayjs";
// hook
const { getDictValue } = useConst();
// 变量
const props = defineProps({
  detail: {
    type: Object,
    default: () => {}
  },
  task: {
    type: Object,
    default: () => {}
  }
});
const viewUserList = ref([]);
const projectId = inject("projectId");
const isEdit = ref(false);
let tableDefault: TableProps = reactive({
  columns: [
    {
      type: "index",
      title: "序号",
      key: "",
      width: 48
    },
    {
      title: "需确认事项",
      key: "content"
    },
    {
      title: "回复",
      key: "replyContent"
    },
    {
      title: "回复人员",
      key: "replyUserId"
    },
    {
      title: "回复时间",
      key: "replyTime"
    },
    {
      title: "操作",
      key: "actionOptions",
      width: 80
    }
  ]
});
let problemList = ref<any>([]);
let schemList = ref({
  data: [],
  columns: tableDefault.columns
});
let itemList = ref({
  data: [],
  columns: tableDefault.columns
});
let taskOrigin = ref({});
const planHead = ref([]);
// 方法
const init = () => {
  schemList.value.data = problemList.value.filter(item => item.typeId === getDictValue("schemeIssue"));
  itemList.value.data = problemList.value.filter(item => item.typeId === getDictValue("ToDoIssue"));
  taskOrigin.value = JSON.parse(JSON.stringify(props.task));
};

//
const getProjectUserListFn = async () => {
  const { data } = await getProjectUserList({ projectId, typeId: getDictValue("planHead") });
  planHead.value = data;
};
// 获取全部用户
const getListAllUserFn = async () => {
  const { data } = await getListAllUser();
  viewUserList.value = data;
};
const editHandler = () => {
  isEdit.value = true;
};
const cancelHandler = () => {
  init();
  isEdit.value = false;
};
const getUserName = id => {
  const item = viewUserList.value.find(item => item.id === id);
  return item?.name || "";
};
const saveHandler = async () => {
  try {
    taskOrigin.value.outcome.forEach(item => {
      delete item.isActive;
      delete item.name;
    });
    const schemLists = schemList.value.data.map(item => {
      return {
        content: item.content,
        replyContent: item.replyContent,
        replyUserId: item.replyUserId,
        replyTime: item.replyTime,
        typeId: getDictValue("schemeIssue"),
        projectId,
        replyUserName: getUserName(item.replyUserId)
      };
    });
    const itemLists = itemList.value.data.map(item => {
      return {
        content: item.content,
        replyContent: item.replyContent,
        replyUserId: item.replyUserId,
        replyTime: item.replyTime,
        typeId: getDictValue("ToDoIssue"),
        projectId,
        replyUserName: getUserName(item.replyUserId)
      };
    });
    await updateProjectTask(taskOrigin.value);
    await getProjectProblemAddList({ projectId, projectProblemModelList: [...schemLists, ...itemLists] });
    mittBus.emit("ProjectFileTime");
    isEdit.value = false;
    refresh();
    ElMessage.success("保存成功");
  } catch (e) {
    console.log(e);
  }
};
const refresh = () => {
  getProjectProblemListFn();
  getTaskDetailFn();
  getProjectUserListFn();
  // mittBus.emit("ProjectDetailRefresh");
};
const deleteAgv = async (id, row) => {
  try {
    await useHandleData(
      () =>
        new Promise(resolve => {
          if (id === 1) {
            schemList.value.data.splice(row.serialNumber, 1);
          } else {
            itemList.value.data.splice(row.serialNumber, 1);
          }

          resolve(true);
        }),
      {},
      "删除"
    );
  } catch (e) {
    console.log(e);
  }
};
const getTaskDetailFn = async () => {
  const { data } = await getTaskDetail({ id: props.task.id });
  taskOrigin.value = data;
};
const getProjectProblemListFn = async () => {
  const { data } = await getProjectProblemList({ projectId });
  problemList.value = data;
  init();
};

onMounted(() => {
  getListAllUserFn();
  getProjectProblemListFn();
  getProjectUserListFn();
});
</script>
<style lang="scss" scoped>
.summary-outline-wrap {
  box-sizing: border-box;
  min-height: 100%;
  padding: 20px;
  background: #ffffff;
  p {
    margin: 0;
  }
  i {
    font-style: normal;
  }
  .mr20 {
    margin-right: 20px;
  }
  .mt10 {
    margin-top: 10px;
  }
  .summary-outline-btn {
    display: flex;
    justify-content: flex-end;
    font-size: 16px;
    font-weight: 400;
    color: var(--el-color-primary);
    .outline-btn-edit {
      display: flex;
      span {
        cursor: pointer;
      }
    }
  }
  .summary-outline-title {
    margin-top: 12px;
    margin-bottom: 60px;
    font-size: 26px;
    font-weight: 600;
    color: #333333;
    text-align: center;
  }
  .summary-outline-content {
    margin: 0 $project-padding;
  }
  .summary-outline-table {
    margin-top: 30px;
    overflow: hidden;
    &-content {
      ul,
      li {
        padding: 0;
        margin: 0;
        list-style: none;
      }
    }
    .table-content-list {
      display: grid;
      grid-template-columns: 1fr 1fr;
      li {
        margin-bottom: 20px;
        font-size: 14px;
        font-weight: 400;
        span:first-child {
          margin-right: 10px;
          color: #666666;
        }
        span:last-child {
          color: #333333;
        }
      }
    }
  }
  .summary-outline-other {
    margin-top: 30px;
  }
  .outline-info {
    ul,
    li {
      padding: 0;
      margin: 0;
      list-style: none;
    }
    &-title {
      margin-bottom: 20px;
      font-size: 16px;
      font-weight: 600;
      color: #333333;
    }
    &-list {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
      column-gap: 10px;
      margin-bottom: 10px !important;
      li {
        display: flex;
        align-items: center;
        font-size: 14px;
        font-weight: 400;
        span:first-child {
          margin-right: 10px;
          color: #666666;
        }
        span:last-child {
          color: #333333;
        }
      }
      .info-width {
        width: 109px;
        text-align: right;
      }
    }
  }
  .summary-outline-custom {
    margin-top: 30px;
    :deep(.el-input),
    :deep(.el-textarea) {
      .el-input__wrapper {
        cursor: default;
        background: $project-background;
        box-shadow: none;
        .el-input__inner {
          cursor: default;
        }
      }
      .el-textarea__inner {
        cursor: default;
        resize: none;
        background: $project-background;
        box-shadow: none;
      }
    }
    :deep(.custom-title-edit) {
      .el-input__wrapper {
        cursor: default;
        background-color: #ffffff;
        box-shadow: none;
        .el-input__inner {
          cursor: default;
        }
      }
      .el-textarea__inner {
        cursor: default;
        background-color: #ffffff;
        box-shadow: none;
      }
    }
  }
  :deep(.el-select) {
    width: 100% !important;
  }
}
</style>
